package com.hll.servlet;

import com.Constants;
import com.hll.exception.ParameterException;
import com.hll.exception.ServiceException;
import com.hll.model.User;
import com.hll.service.UserService;
import com.hll.util.StringUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@WebServlet("/login.action")
public class LoginServlet extends javax.servlet.http.HttpServlet {

    private final String LOGIN_PAGE = "/WEB-INF/jsp/login.jsp";

    @Override
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

        String userName = request.getParameter("userName");
        String password = request.getParameter("password");

        /**连接数据库并取数据*/
        try {
            User user = null;
            UserService userService = new UserService();
            user = userService.login(userName, password);
            user.setPassword(null);
            HttpSession session = request.getSession();
            session.setAttribute(Constants.USER, user);

            /**这个是获取login.jsp的隐藏域参数*/
            String go = request.getParameter("go");
            String queryString = request.getParameter("queryString");

            if(!StringUtil.isEmpty(queryString)) {
                if(queryString.startsWith("#")) {
                    //去掉#号
                    queryString = queryString.substring(1);
                }
                go = go + "?" + queryString;
            }

            /**若go为空，则返回正常进入时的界面，即我的图书界面，若不为空则返回上次异常退出的界面*/
            if(StringUtil.isEmpty(go)) {
                response.sendRedirect(request.getContextPath() + "/myBook.action");
            } else {
                /**若不为空则默认进入上次异常退出的界面，url后挂上参数go即上次异常退出的url*/
                response.sendRedirect(request.getContextPath() + "/" + go);
            }

        } catch (ParameterException parameterException) {

            /**验证数据是否为空*/
            Map<String, String> errorFields = parameterException.getErrorFields();
            request.setAttribute(Constants.ERROR_FIELDS, errorFields);
            request.getRequestDispatcher(LOGIN_PAGE).forward(request, response);
        } catch (ServiceException serviceException) {

            request.setAttribute(Constants.TIP_MESSAGE, serviceException.getMessage() + "[" + serviceException.getCode() + "]");
            request.getRequestDispatcher(LOGIN_PAGE).forward(request, response);
        }
    }

    @Override
    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

        HttpSession session = request.getSession();
        User user = (User)session.getAttribute(Constants.USER);

        /**如果用户登录过了，直接进入主页面，无需进入login.jsp*/
        if (user != null) {
            response.sendRedirect(request.getContextPath() + "/myBook.action");
        } else {
            String go = request.getParameter("go");
            if (StringUtil.isEmpty(go)) {
                go = "";
            }
            /**放入url里的go参数，设置到login.jsp的隐藏域中并转向登陆界面，
             * 在后续登录界面登陆时，会带上go参数登录，进入post方法，再对参数处理
             */
            request.setAttribute("go", go);
            request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
        }
    }
}